package com.example.mybatis.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatis.bean.TestScore;
import com.example.mybatis.service.TestScoreService;
import com.example.mybatis.mapper.TestScoreMapper;
import com.example.mybatis.utils.ResultUtil;
import com.example.mybatis.utils.validator.DataValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 35050
* @description 针对表【test_score】的数据库操作Service实现
* @createDate 2025-03-08 09:58:06
*/
@Service
public class TestScoreServiceImpl extends ServiceImpl<TestScoreMapper, TestScore>
    implements TestScoreService{
    @Resource
    TestScoreMapper testScoreMapper;

    public ResultUtil selectList(Double testScore,Integer current,Integer size){
        QueryWrapper<TestScore> queryWrapper = new QueryWrapper<>();
        //eq：等于
        //queryWrapper.eq("status",0);
/*        //动态sql语句
        .eq(testScore != null ,"test_score", testScore)
        //传递数组 Object... values
        .in("test_score",79,90,100)
        //传递集合Collection<?> coll
         .in("test_score", CollUtil.newArrayList(79,90,100));*/

        /*表达式写法*/
        LambdaQueryWrapper<TestScore> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TestScore::getStatus,"0")
                .eq(testScore != null ,TestScore::getTestScore, testScore)
                .in(TestScore::getTestScore, 79,90,100)
                .in(TestScore::getTestScore, CollUtil.newArrayList(79,90,100));



        //List<TestScore> testScoreList = testScoreMapper.selectList(queryWrapper);

       IPage<TestScore> testScoreIPage=testScoreMapper.selectPage(new Page<>(current,size),queryWrapper);
        //IPage<Score> scoreIPage = scoreMapper.selectAll(new Page<>(current,size),score);

        return ResultUtil.ok()
                //页数
                .put("dataList", testScoreIPage.getRecords())
                //条数
                .put("total", testScoreIPage.getTotal());
    }

    @Override
    public ResultUtil insert(TestScore testScore) {
        DataValidator.validator(testScore);
        testScoreMapper.insert(testScore);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil update(TestScore testScore) {
        LambdaUpdateWrapper<TestScore> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TestScore::getSafetyEducationId,testScore.getSafetyEducationId());
        testScoreMapper.update(testScore,lambdaUpdateWrapper);

        return ResultUtil.ok();
    }


}




